package com.hontech.bv.ui.fragment.heat

import android.view.Gravity
import android.widget.Button
import com.hontech.bv.R
import com.hontech.bv.model.heat.HeatStatus
import com.hontech.bv.model.heat.Status
import com.hontech.bv.model.ice.Sw
import com.hontech.bv.ui.AutoHideKeyActivity
import com.hontech.bv.ui.view.NumberSensorView
import com.hontech.bv.ui.view.SwitchSensorView
import com.hontech.bv.ui.window.MoveableWindow
import com.hontech.bv.utils.dimenIntById

private val width = dimenIntById(R.dimen.w720)
private val height = dimenIntById(R.dimen.h940)

class HeatSensorDialog : MoveableWindow(R.layout.dialog_heat_sensor, width, height, false) {

    companion object {

        private var INSTANCE: HeatSensorDialog? = null

        fun show() {
            if (INSTANCE == null) {
                INSTANCE = HeatSensorDialog()
            }
            INSTANCE!!.show()
        }

        fun update(status: Status) {
            INSTANCE?.update(status)
        }
    }

    private val btnClose = view.findViewById<Button>(R.id.id_heat_sensor_btn_close)

    private val stirTop = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_stir_top)
    private val stirBottom = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_stir_bottom)
    private val waterLeft = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_water_left)
    private val waterRight = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_water_right)
    private val iceLeft = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_ice_left)
    private val iceRight = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_ice_right)
    private val leak = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_leak)
    private val bucketHigh = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_bucket_high)
    private val bucketLow = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_bucket_low)
    private val outletHigh = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_outlet_high)
    private val outletLow = view.findViewById<SwitchSensorView>(R.id.id_heat_sensor_ssv_outlet_low)
    private val numWater = view.findViewById<NumberSensorView>(R.id.id_heat_sensor_num_water_temp)
    private val numSteam = view.findViewById<NumberSensorView>(R.id.id_heat_sensor_num_steam_temp)

    init {
        btnClose.setOnClickListener {
            dismiss()
            INSTANCE = null
        }
    }

    fun show() {
        window.showAtLocation(AutoHideKeyActivity.parentView(), Gravity.CENTER, 0, 0)
        update(HeatStatus.status)
    }

    private fun update(status: Status) {
        val sw = status.sw()
        stirTop.set(sw.stirTop)
        stirBottom.set(sw.stirBottom)
        waterLeft.set(sw.heatLeft)
        waterRight.set(sw.heatRight)
        iceLeft.set(sw.iceLeft)
        iceRight.set(sw.iceRight)
        leak.set(sw.leak)
        bucketHigh.set(sw.bucketHigh)
        bucketLow.set(sw.bucketLow)
        outletHigh.set(sw.outletHigh)
        outletLow.set(sw.outletLow)

        if (sw.waterTempOk) {
            numWater.set(status.waterTemp.signalValue())
        } else {
            numWater.set("异常")
        }

        if (sw.steamTempOk) {
            numSteam.set(status.steamTemp.signalValue())
        } else {
            numSteam.set("异常")
        }

    }
}

